package com.jeecg.p3.system.web.back;

import com.jeecg.p3.system.entity.JwSystemRole;
import com.jeecg.p3.system.entity.ProductCategory;
import com.jeecg.p3.system.service.ProductCategoryService;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @program: jeewx-boot-parent
 * @description: 商品类别控制处理类
 * @author: binbin看
 * @create: 2020-09-28 09:49
 */
@Controller
@RequestMapping("/system/back/product/category")
public class ProductCategoryController extends BaseController {

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 列表页面
     * @return
     */
    @RequestMapping(value="list",method = {RequestMethod.GET,RequestMethod.POST})
    public void list(@ModelAttribute ProductCategory query, HttpServletResponse response, HttpServletRequest request,
                     @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
                     @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
        PageQuery<ProductCategory> pageQuery = new PageQuery<ProductCategory>();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        VelocityContext velocityContext = new VelocityContext();
        pageQuery.setQuery(query);
        velocityContext.put("productCategory",query);
        velocityContext.put("pageInfos", SystemTools.convertPaginatedList(productCategoryService.queryPageList(pageQuery)));
        String viewName = "system/back/productCategory-list.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 详情
     * @return
     */
    @RequestMapping(value="toDetail",method = RequestMethod.GET)
    public void jwSystemRoleDetail(@RequestParam(required = true, value = "id" ) Long id,HttpServletResponse response,HttpServletRequest request)throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        String viewName = "system/back/productCategory-detail.vm";
        ProductCategory productCategory = productCategoryService.queryById(id);
        velocityContext.put("productCategory",productCategory);
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping(value = "/toAdd",method ={RequestMethod.GET, RequestMethod.POST})
    public void toAddDialog(HttpServletRequest request, HttpServletResponse response, ModelMap model)throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        String viewName = "system/back/productCategory-add.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 保存信息
     * @return
     */
    @RequestMapping(value = "/doAdd",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doAdd(@ModelAttribute ProductCategory productCategory){
        AjaxJson json = new AjaxJson();
        try {
            productCategoryService.doAdd(productCategory);
            json.setMsg("保存成功");
        } catch (Exception e) {
            log.info(e.getMessage());
            json.setSuccess(false);
            json.setMsg("保存失败");
        }
        return json;
    }

    /**
     * 跳转到编辑页面
     * @return
     */
    @RequestMapping(value="toEdit",method = RequestMethod.GET)
    public void toEdit(@RequestParam(required = true, value = "id" ) Long id,HttpServletResponse response,HttpServletRequest request) throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        ProductCategory productCategory = productCategoryService.queryById(id);
        velocityContext.put("productCategory",productCategory);
        String viewName = "system/back/productCategory-edit.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/doEdit",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doEdit(@ModelAttribute ProductCategory productCategory){
        AjaxJson j = new AjaxJson();
        try {
            productCategoryService.doEdit(productCategory);
            j.setMsg("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("编辑失败");
        }
        return j;
    }


    /**
     * 删除
     * @return
     */
    @RequestMapping(value="doDelete",method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson doDelete(@RequestParam(required = true, value = "id" ) Long id){
        AjaxJson j = new AjaxJson();
        try {
            ProductCategory productCategory = productCategoryService.queryById(id);
            productCategoryService.doDelete(id);
            j.setMsg("删除成功");
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("删除失败");
        }
        return j;
    }

    /**
     * 跳转到编辑页面
     * @return
     * @throws Exception
     */
    @RequestMapping(value="toPrivilegeTree",method = RequestMethod.GET)
    public void toPrivilegeTree(@RequestParam(required = true, value = "id" ) Long id, HttpServletResponse response, HttpServletRequest request) throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        ProductCategory productCategory = productCategoryService.queryById(id);
        velocityContext.put("productCategory",productCategory);
        String viewName = "system/back/productCategory-edit.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }




}
